<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="1-5个字符" />
      </el-form-item>

      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="1-5个字符" />
      </el-form-item>

      <el-form-item label="部门负责" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择活动区域">
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
          <!-- <el-option label="区域二" value="beijing" /> -->
        </el-select>
      </el-form-item>

      <el-form-item label="部门介绍">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    cExistedName: {
      type: Array,
      required: true
    },
    existedCodeList: {
      type: Array,
      required: true
    },
    pid: {
      type: String,
      required: true
    },
    // 是编辑不是添加相当于修改
    isEdit: {
      type: Boolean,
      required: true
    }

  },
  data() {
    const validCode = (rule, value, callback) => {
      console.log('要校验的值', value)
      console.log('已经被占用code', this.existedCodeList)
      //   if(this.existedCodeList.includes(value)){
      //       callback(new Error(`部门编号 ${value} 已占用`))
      //   }
      //   如果当前是编辑，则需要将当前编码从已经占用的编码列表中排除在外
      // 如果当前就是编辑，list就是this.existedCodeList.includes(value) ? callback(new Error(`部门编号 ${value} 已占用`)) : callback()
      // 不是编辑就是this.existedCodeList
      const list = this.isEdit
        ? this.existedCodeList.filter(item => item !== this.code)
        : this.existedCodeList

      list.includes(value) ? callback(new Error(`部门编号 ${value} 已占用`))
        : callback()
      console.log(list, value)

    // if(this.isEdit){
    //    const list = this.existedCodeList.filter(item => item !== this._code)
    //    console.log('已经占用的code是',list);
    //    list.includes(value) ? callback(new Error(`部门编号 ${value} 已占用`)) : callback()
    // }else{
    //     this.existedCodeList.includes(value) ? callback(new Error(`部门编号 ${value} 已占用`)) : callback()
    // }
    //   this.existedCodeList.includes(value) ? callback(new Error(`部门编号 ${value} 已占用`)) : callback()
    }

    const validName = (rule, value, callback) => {
      const list = this.isEdit
        ? this.cExistedName.filter(item => item !== this.name)
        : this.cExistedName
      console.log('要校验的值', value)
      console.log('已经被占用code', this.cExistedName)
      list.includes(value) ? callback(new Error(`部门编号 ${value} 已占用`)) : callback()
    }
    return {
      // _code:'',
      peoples: [],
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要1-5字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    }
  },
  async created() {
    console.log('addorEdit组件被创建')
    this.loadEmployee()
    if (this.isEdit) {
      // 获取详情
      const rs = await getDepartDetail(this.pid)
      this.form = rs.data
      console.log('现在是编辑状态', rs.data.depts)
      //   保存当前部门编码
      //   如果把——code定义在data中代码一样可以运行，区别在于data中的数据都有响应式效果
      // 修改了这个值视图会更新
      // 对于_code，不需要他具备这样的能力，
      this.code = rs.data.code
      this.name = rs.data.name
      console.log(this.name)
    }
  },
  methods: {
    async loadEmployee() {
      const rs = await getEmployeeSimple()
      console.log('员工信息', rs)
      this.peoples = rs.data
    },
    // 执行具体添加动作
    async doAdd() {
      // 组装参数
      const params = { ...this.form, pid: this.pid }
      console.log(params)
      //   调用接口
      const rs = await addDepartments(params)
      console.log(rs)
      this.$emit('update-depart')
    //   通知父组件关闭弹层
    },
    async doEdit() {
      // 调用接口，这里的this.form中已经有所需要传递的参数了
      const rs = await updateDepartments(this.form)
      console.log('编辑操作的返回结果', rs)
      //   通知父组件，关闭弹层，再次更新数据
      this.$emit('update-depart')
    },
    onSubmit() {
      // 表单验证
      this.$refs.form.validate(valid => {
        if (!valid) {
          alert('错误')
          return
        }
        this.isEdit ? this.doEdit() : this.doAdd()
        // if (valid) {
        //   console.log('submit!')
        //   if (this.isEdit) {
        //     this.doAdd()
        //   } else {
        //     this.doAdd()
        //   }
        // } else {
        //   alert('错误')
        // }
      })
    },
    hCancel() {
      // 通知父组件去关闭弹层
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
